package cn.htsyb.synergy.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 *
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
public class ThreadStoreUtil {
    private static final InheritableThreadLocal<Map> threadLocal = new InheritableThreadLocal<Map>() {
        @Override
        protected Map initialValue() {
            return new ConcurrentHashMap<>();
        }
    };

    public static Map<Object, Object> get() {
        return threadLocal.get();
    }

    public static boolean set(Map<Object, Object> state) {
        clear();
        putAll(state);
        return true;
    }

    public static Object get(Object key) {
        return threadLocal.get().get(key);
    }

    public static Object remove(Object key) {
        return threadLocal.get().remove(key);
    }

    public static boolean clear() {
        threadLocal.get().clear();
        return true;
    }

    public static boolean put(Object key, Object value) {
        threadLocal.get().put(key, value);
        return true;
    }

    public static boolean putAll(Map<Object, Object> state) {
        threadLocal.get().putAll(state);
        return true;
    }
}